package com.server.mcp;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.server.core.McpTool;
import com.server.module.demo.domain.Demo;
import com.server.module.demo.service.IDemoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author WuFengSheng
 * @date 2025/4/19 10:03
 */
@Slf4j
@Service
@AllArgsConstructor
public class McpDemoService implements McpTool {

    private final IDemoService demoService;

    @Tool(description = "根据年龄查询 Demo 列表")
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public String queryDemoInfoByAge(@ToolParam(description = "年龄") Integer age) {
        Page<Demo> page = new Page<>(1, 10);
        List<Demo> demoList = demoService.list(page, Wrappers.<Demo>lambdaQuery().eq(Demo::getAge, age).orderByAsc(Demo::getName));
        return JSON.toJSONString(demoList);
    }

}
